import pandas as pd # 导入数据分析模块
import numpy as np # 导入科学计算模块
from pyecharts.charts import Bar, Map, Line, Pie, Grid, Timeline, WordCloud, Map3D, \
    MapGlobe  # 导入条形图,地图,折线图,玫瑰图 ,grid并行组图组件
from pyecharts import options as opts # 导入配置项模块
from pyecharts.globals import ThemeType # 导入主题模块



def draw_mix(data,map_name,data_bar_today,data_bar_total,data_pie,data_word,data_map_today,data_map_total,type,line1,market1,line2,market2,line3,market3,):
    t1 = Timeline(init_opts=opts.InitOpts(theme=ThemeType.CHALK,width='1536px',height='672px'))
    for z, date in enumerate(data):
        pieces = [
            {"min": 10000000},
            {"min": 1000000, "max": 9999999},
            {"min": 100000, "max": 999999},
            {"min": 10000, "max": 99999},
            {"min": 1000, "max": 9999},
            {"min": 0, "max": 999},
        ]
    # 条形图
        bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add_xaxis([i[0] for i in data_bar_today[date][:30]])
            .add_yaxis('新增确诊', [i[1] for i in data_bar_today[date]][:30])
            .add_yaxis('累计确诊', [i[1] for i in data_bar_total[date]][:30])
            .set_global_opts(title_opts=opts.TitleOpts(title=map_name + '累计确诊人数条形图',
                                                      pos_left='7%', pos_top='1.5%',
                                                      title_textstyle_opts=opts.TextStyleOpts(font_size=14),

                                                      ),
                            legend_opts=opts.LegendOpts(pos_top='4.6%', pos_left='6.5%', is_show=True,
                                                        textstyle_opts=opts.TextStyleOpts(color='white')),
                            datazoom_opts=opts.DataZoomOpts(is_show=True, range_start=0, range_end=20, pos_top='43%', ),
                            xaxis_opts=opts.AxisOpts(name='',
                                                     type_='category',  # 轴的类型
                                                     name_location='center',  # 坐标轴轴名称的位置
                                                     name_gap=10,  # 坐标轴轴名称距离轴线的距离
                                                     axislabel_opts=opts.LabelOpts(color='white', is_show=True),
                                                     # 坐标轴标签的颜色

                                                     axistick_opts=opts.AxisTickOpts(is_show=True,  # 是否显示坐标轴刻度线
                                                                                     is_inside=True,  # 刻度线是否朝内
                                                                                     # 坐标轴刻度线的样式
                                                                                     linestyle_opts=opts.LineStyleOpts(
                                                                                         color='cyan')),
                                                     axisline_opts=opts.AxisLineOpts(symbol=['none', 'arrow'],
                                                                                     # 坐标轴的箭头的方向
                                                                                     # 坐标轴轴线的颜色
                                                                                     linestyle_opts=opts.LineStyleOpts(
                                                                                         color='cyan'))
                                                     ),
                            yaxis_opts=opts.AxisOpts(name='人数',  # x轴的名称
                                                     type_='value',  # 轴的类型
                                                     name_location='center',  # 坐标轴轴名称的位置
                                                     name_gap=10,  # 坐标轴轴名称距离轴线的距离
                                                     axislabel_opts=opts.LabelOpts(color='cyan', is_show=False),
                                                     # 坐标轴标签的颜色
                                                     axistick_opts=opts.AxisTickOpts(is_show=True,  # 是否显示坐标轴刻度线
                                                                                     is_inside=True,  # 刻度线是否朝内
                                                                                     # 坐标轴刻度线的样式
                                                                                     linestyle_opts=opts.LineStyleOpts(
                                                                                         color='cyan')),
                                                     axisline_opts=opts.AxisLineOpts(symbol=['none', 'arrow'],
                                                                                     # 坐标轴的箭头的方向
                                                                                     # 坐标轴轴线的颜色
                                                                                     linestyle_opts=opts.LineStyleOpts(
                                                                                         color='cyan'))
                                                     ),
                            # ----- 视觉映射配置项目
                            visualmap_opts=opts.VisualMapOpts(is_piecewise=True,
                                                              is_show=False,
                                                              range_text=['Highe', 'Low'],
                                                              textstyle_opts=opts.TextStyleOpts(color='cyan'),

                                                              # 设置分段颜色
                                                              max_=1000000,  # 最大值
                                                              pos_right='1%',
                                                              pos_top='center',
                                                              pieces=pieces,
                                                              ),


                            )
           )


    # 玫瑰图
        pie = (Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add('', data_pie[date][:13],
                 radius=['40', '83'],
                 rosetype='area',
                 center=['19.3%', '73%'],
                label_opts=opts.LabelOpts(formatter="{b}:{c}人"),
                )
            .set_global_opts(title_opts=opts.TitleOpts(title=map_name + '累计确诊玫瑰图',
                                                       pos_left='14.5%',
                                                       title_textstyle_opts=opts.TextStyleOpts(font_size=14),
                                                       pos_top='53%'),
                            legend_opts=opts.LegendOpts(is_show=False))

           )
        # 地图


        name = pd.read_csv('translate.csv',encoding='utf-8')
        name_C ={i:j for i,j in zip([a for a in name['English']],[b for b in name['chinese']])}

        map_ = (Map(init_opts=opts.InitOpts(theme=ThemeType.CHALK,width='400px',height='400px'))
                 .add('today_confirm', data_map_today[date], type,
                      label_opts=opts.LabelOpts(color='#fcfcfc', font_weight='bold', font_size=12,
                                                font_family='SimHei'), is_map_symbol_show=False,name_map=name_C,zoom=0.73,center=[17,8])
                 .add('total_confirm', data_map_total[date], type,
                      label_opts=opts.LabelOpts(color='#fcfcfc', font_weight='bold', font_size=12,
                                                font_family='SimHei'), is_map_symbol_show=False,name_map=name_C)
                .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
                 .set_global_opts(title_opts=opts.TitleOpts(map_name + '疫情地图', pos_left='center',
                                                            title_textstyle_opts=opts.TextStyleOpts(font_size=20, ),
                                                            subtitle_textstyle_opts=opts.TextStyleOpts(font_size=16,
                                                                                                       color='white'),
                                                            pos_top='2%'),
                                  legend_opts=opts.LegendOpts(pos_top='7%',
                                                              textstyle_opts=opts.TextStyleOpts(color='white'),
                                                              is_show=True,selected_mode='single'),
                                  visualmap_opts=opts.VisualMapOpts(max_=1000000,is_piecewise=True, pieces=pieces, pos_left='5%',
                                                                    pos_top='center',textstyle_opts=opts.TextStyleOpts(color='white')),
                                  )
                 )

        #词云shape='square',
        couldword = (
            WordCloud(init_opts=opts.InitOpts(theme=ThemeType.CHALK,))
                .add('', data_pair=data_word[date], word_size_range=[20, 45],
                     textstyle_opts=opts.TextStyleOpts(font_family='Microsoft YaHei', font_weight='bold', ),
                     pos_left='73%', pos_right='3%', pos_top='10%', pos_bottom='75%', height='240px', width='370px',)
                .set_global_opts(title_opts=opts.TitleOpts(title="疫情热点",
                                                           title_textstyle_opts=opts.TextStyleOpts(font_size=15),pos_right='13%',
                                                           pos_top='4%'))
        )

        line = (Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))
                .add_xaxis([i[0] for i in line1])
                .add_yaxis('总计确诊', [int(i[1]) for i in line1],
                       symbol='circle',
                       symbol_size=5,
                       label_opts=opts.LabelOpts(is_show=False),
                       is_smooth=True,
                       )
                .add_yaxis('总计确诊', market1[z],  # 气泡系列数据,
                       label_opts=opts.LabelOpts(is_show=False),
                       symbol_size=0,
                       linestyle_opts=opts.LineStyleOpts(is_show=False, width=0),
                       markpoint_opts=opts.MarkPointOpts(symbol_size=[40, 40],
                                                         data=[opts.MarkLineItem(type_='max')])
                       )
                .add_yaxis('累计治愈', [int(i[1]) for i in line2],
                           symbol='circle',
                           symbol_size=5,
                           label_opts=opts.LabelOpts(is_show=False),
                           is_smooth=True,
                           )
                .add_yaxis('累计治愈', market2[z],  # 气泡系列数据,
                           label_opts=opts.LabelOpts(is_show=False),
                           symbol_size=0,
                           linestyle_opts=opts.LineStyleOpts(is_show=False, width=0),
                           markpoint_opts=opts.MarkPointOpts(symbol_size=[40, 40],
                                                             data=[opts.MarkLineItem(type_='max')])
                           )
                .add_yaxis('累计死亡', [int(i[1]) for i in line3],
                           symbol='circle',
                           symbol_size=5,
                           label_opts=opts.LabelOpts(is_show=False),
                           is_smooth=True,
                           )
                .add_yaxis('累计死亡', market3[z],  # 气泡系列数据,
                           label_opts=opts.LabelOpts(is_show=False),
                           symbol_size=0,
                           linestyle_opts=opts.LineStyleOpts(is_show=False, width=0),
                           markpoint_opts=opts.MarkPointOpts(symbol_size=[40, 40],
                                                             data=[opts.MarkLineItem(type_='max')])
                           )
            .set_global_opts(
                                        title_opts=opts.TitleOpts(title=map_name+'时序折线图',
                                                          pos_right='11%',
                                                          title_textstyle_opts=opts.TextStyleOpts(font_size=14),pos_top='49%'),

                                     xaxis_opts=opts.AxisOpts(name='日期',
                                                              name_gap=20,
                                                              name_textstyle_opts=opts.TextStyleOpts(color='cyan',
                                                                                                     font_size=10),
                                                              axislabel_opts=opts.LabelOpts(color='cyan'),
                                                              axisline_opts=opts.AxisLineOpts(is_show=True,
                                                                                              symbol=['none', 'arrow'],


                                                                                              linestyle_opts=opts.LineStyleOpts(
                                                                                                  color='cyan')),
                                                              ),
                                     yaxis_opts=opts.AxisOpts(name='人数',
                                                              min_=0,
                                                              name_location='center',
                                                                name_gap=10,
                                                              name_textstyle_opts=opts.TextStyleOpts(color='cyan',
                                                                                                     ),
                                                              axislabel_opts=opts.LabelOpts(color='cyan',is_show=False),
                                                              axisline_opts=opts.AxisLineOpts(is_show=True,
                                                                                              #  设置轴线的颜色
                                                                                              linestyle_opts=opts.LineStyleOpts(
                                                                                                  color='cyan'),symbol=['none', 'arrow']),



                                                              ),
                                     tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'),
                                     axispointer_opts=opts.AxisPointerOpts(is_show=True,
                                                                           label=opts.LabelOpts(color='black',
                                                                                                font_size=12,
                                                                                                font_family='SimHei')),
                                     legend_opts=opts.LegendOpts(is_show=True,pos_bottom='44%',pos_right='6%'),


                                     )
                    .set_series_opts(linestyle_opts=opts.LineStyleOpts(color='pink',
                                                                       width=2,
                                                                       ))
                    )

    # pos_left='67%', pos_right='8%', pos_bottom='55%', pos_top='8%'
        grid = (Grid(init_opts=opts.InitOpts(theme=ThemeType.CHALK, width='1536px',height='672px'))
                .add(bar,
                 grid_opts=opts.GridOpts(pos_left='2.6%',pos_right='77.6%',pos_top='11.5%',pos_bottom='57%'),
                 is_control_axis_index=True,
                 )
                .add(line,
                 grid_opts=opts.GridOpts(pos_left ='75.2%', pos_right = '4%', pos_top ='57%', pos_bottom = '10.5%'),
                 )
                .add(pie,
                 grid_opts=opts.GridOpts()
                 )
                .add(map_,
                 grid_opts=opts.GridOpts(is_contain_label=True))
                .add(couldword,grid_opts=opts.GridOpts())
            )
        t1.add(grid, f'{date}')
        t1.add_schema(play_interval=1000,
                  is_loop_play=False,
                  orient='horizontal',
                  pos_left='8%',
                  pos_bottom='0.8%',
                  pos_right='10%'

                  )
    t1.render(map_name+'.html')

data = pd.read_csv('world_all_country_history_data_2022_07_19.csv')
data_m = pd.read_csv('xiugai_2022_07_27.csv')
data['date'] = pd.to_datetime(data['date'])
data_m['date'] = pd.to_datetime((data_m['date']))
data_m.set_index('date',inplace=True)
data.set_index('date',inplace=True)
#对整体时间进行处理
date_list  = pd.DataFrame(pd.date_range('2021-01-1','2022-07-01',freq='7D'))
date_list = date_list[0].apply(lambda x:x.strftime('%Y-%m-%d'))


data_list_total = {}
data_list_today = {}
data_list_total_num = {}
data_list_dead_num = {}
data_list_heal_num = {}

for i in date_list:
    data_list_total[i] = data_m.loc[i, ['country_name', 'total_confirm']].sort_values(by='total_confirm').values.tolist()[::-1]
    data_list_today[i] = data_m.loc[i, ['country_name', 'today_confirm']].values.tolist()[::-1]
    data_list_total_num[i] = data.loc[i, ['total_confirm']].sum().values.tolist()
    data_list_dead_num[i] = data.loc[i, ['total_dead']].sum().values.tolist()
    data_list_heal_num[i] = data.loc[i, ['total_heal']].sum().values.tolist()


#数据准备
key1 = [ i for i in data_list_heal_num.keys()]
value1 = [ i[0] for i in data_list_heal_num.values()]
data_list_heal_num_ = [[key1[i],value1[i]] for i in range(len(key1))]

key2 = [ i for i in data_list_dead_num.keys()]
value2 = [ i[0] for i in data_list_dead_num.values()]
data_list_dead_num_ = [[key2[i],value2[i]] for i in range(len(key2))]

key3 = [ i for i in data_list_heal_num.keys()]
value3 = [ i[0] for i in data_list_heal_num.values()]
data_list_total_num_ = [[key3[i],value3[i]] for i in range(len(key3))]

#气泡数据准备
marker_data = []
marker_data_h = []
marker_data_d = []
for i,j  in enumerate(date_list):
    md_data =  [0] * len(data_list_total_num_)
    mdh_data = [0] * len(data_list_heal_num_)
    mdd_data = [0] * len(data_list_dead_num_)
    md_data[i] = int(dict(data_list_total_num_)[j])
    mdh_data[i] = int(dict(data_list_heal_num_)[j])
    mdd_data[i] = int(dict(data_list_dead_num_)[j])
    marker_data.append(md_data)
    marker_data_h.append(mdh_data)
    marker_data_d.append(mdd_data)


line1 = data_list_total_num_
market1 = marker_data
line2 = data_list_heal_num_
market2 = marker_data_h
line3 = data_list_dead_num_
market3 = marker_data_d




draw_mix(data=date_list,map_name='世界',data_bar_today=data_list_today,data_bar_total=data_list_total,data_pie=data_list_total,data_word=data_list_today,
         data_map_today=data_list_today,data_map_total=data_list_total,type='world',line1=data_list_total_num_,market1=marker_data,
         line2=data_list_heal_num_,market2=marker_data_h,line3=data_list_dead_num_,market3=marker_data_d)